package com.compass.mobile.ui.dialog

import AppManager
import android.view.Gravity
import android.view.LayoutInflater
import androidx.fragment.app.FragmentActivity
import com.compass.mobile.callback.OnCustomButtonClick
import com.compass.mobile.databinding.DialogInquiryBinding
import com.compass.mobile.framework.base.BaseDialog
import com.compass.mobile.framework.base.BaseDialogFragment
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.models.EquityBean

/**
 * Description： 问诊弹窗
 * Date:2024/7/16 14:36
 */
class InquiryDialog {
    class Builder(
        activity: FragmentActivity,
        private var benefits: MutableList<EquityBean>?,
        private var onCustomButtonClick: OnCustomButtonClick?,
        private var inquiryType: String
    ) :
        BaseDialogFragment.Builder<Builder>(activity) {
        private val mBinding: DialogInquiryBinding =
            DialogInquiryBinding.inflate(LayoutInflater.from(activity))
        private var benefitId: String? = null

        init {
            initView()
        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth((AppManager.getScreenHeightPx() * 0.8).toInt())
            setHeight((AppManager.getScreenWidthPx() * 0.6).toInt())
            setAnimStyle(BaseDialog.AnimStyle.DEFAULT)
            setGravity(Gravity.CENTER)
            setCancelable(true)
            FocusViewUtils.scaleView(mBinding.llCode, 0)
            FocusViewUtils.scaleView(mBinding.llEquityOrder, 0)
            FocusViewUtils.scaleView(mBinding.llEquityWriteOff, 0)
            mBinding.llEquityOrder.onClick {
                //订购
                onCustomButtonClick?.onButtonCallBack(1, null)
                dismiss()

            }
            mBinding.llEquityWriteOff.onClick {
                //核销
                onCustomButtonClick?.onButtonCallBack(2, benefitId)
                dismiss()
            }
            mBinding.llCode.onClick {
                //扫码
                onCustomButtonClick?.onButtonCallBack(3, null)
                dismiss()

            }
            setEquity()
        }

        private fun setEquity() {
            if (benefits.isNullOrEmpty()) {
                mBinding.llEquityWriteOff.gone()
            } else {
                mBinding.llEquityWriteOff.visible()
                val item = benefits?.get(0)
                item?.let {
                    benefitId = it.id
                    mBinding.tvEquity.text = "${it.benefitName}数量：${getNumber(item)}"
                }
            }
        }

        private fun getNumber(equityBean: EquityBean): String? {
            // type={1图文 2电话 3视频}

            return when (inquiryType) {
                "1" -> equityBean.txtNumber
                "2" -> equityBean.phoneNumber
                "3" -> equityBean.videoNumber
                else -> {
                    "0"
                }
            }
        }

        fun dismissDialog() {

            dismiss()
        }
    }
}